﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using SCMS.Model;
using SCMS.CoreBusinessLogic.TenderAnalysis;

namespace SCMS.UI.Models
{
    public class TAnalysis
    {

        public SCMS.Model.TenderBidder EntityTenderBidder { get; set; }


        public string SupplierName
        {
            get { return EntityTenderBidder.Supplier.Name; }
            set { EntityTenderBidder.Supplier.Name = value; }
        }

        [Required]
        public Guid SupplierID
        {
            get { return EntityTenderBidder.SupplierId; }
            set { EntityTenderBidder.SupplierId = value; }
        }

        public List<SCMS.Model.TenderBidderQuote> TBiderQuotes { get; set; }

        public string ShippingTerm
        {
            get { return EntityTenderBidder.ShippingTerm.Name; }
            set { EntityTenderBidder.ShippingTerm.Name = value; }
        }

        public DateTime? DeliveryTime
        {
            get { return EntityTenderBidder.DeliveryTime; }
            set { EntityTenderBidder.DeliveryTime = value; }
        }

        [Display(Name = "Warranty")]
        public string Warrant
        {
            get { return EntityTenderBidder.Warranty; }
            set { EntityTenderBidder.Warranty = value; }
        }

        [Display(Name = "Terms of Payment")]
        public string TermzOfPayment
        {
            get { return EntityTenderBidder.PaymentTerm.Description; }
            set { EntityTenderBidder.PaymentTerm.Description = value; }

        }
        [Display(Name = "Transport Cost")]
        public decimal? TransportCost
        {
            get { return EntityTenderBidder.TransportCost; }
            set { EntityTenderBidder.TransportCost = value; }
        }

        [Display(Name = "Total cost")]
        public decimal? TotalCost
        {
            get { return EntityTenderBidder.TotalCost; }
            set { EntityTenderBidder.TotalCost = value; }
        }

        public Guid OtherID
        {
            get { return EntityTenderBidder.Id; }
            set { EntityTenderBidder.Id = value; }
        }

        [Display(Name = "Others")]
        public string others
        {

            get { return EntityTenderBidder.Comments; }
            set { EntityTenderBidder.Comments = value; }

        }

    }

    public class TAORequest
    {

        public SCMS.Model.TenderAnalysi EntityTenderAnalysis { get; set; }

        [Required(ErrorMessage = "Required Field")]
        public string TenderAnalisysNo
        {
            get { return EntityTenderAnalysis.RefNumber; }
            set { EntityTenderAnalysis.RefNumber = value; }
        }

        [Required(ErrorMessage = "Required Field")]
        [DataType(DataType.Date)]
        [Display(Name = "Issue Date")]
        public DateTime TADate
        {
            get { return EntityTenderAnalysis.AnalysisDate; }
            set { EntityTenderAnalysis.AnalysisDate = value; }
        }

        [Required(ErrorMessage = "Required Field")]
        public Guid OrderRequestID
        {
            get { return EntityTenderAnalysis.OrderRequestId; }
            set { EntityTenderAnalysis.OrderRequestId = value; }
        }
        public SelectList OrderRequests { get; set; }

        public bool EditMode { get; set; }

        public bool SupplierAdded { get; set; }
    }

    public class AddTAnalysis
    {
        public SCMS.Model.TenderBidder EntityTenderBidder { get; set; }

        [Required]
        public Guid SupplierID
        {
            get { return EntityTenderBidder.SupplierId; }
            set { EntityTenderBidder.SupplierId = value; }
        }
        public SelectList Suppliers { get; set; }


        public List<RequestItems> RequestItems { get; set; }


        [Required]
        public Guid? ShippingTermID
        {
            get { return EntityTenderBidder.ShippingTermId; }
            set { EntityTenderBidder.ShippingTermId = value; }
        }
        public SelectList ShippingTerms { get; set; }


        [Required]
        [Display(Name = "Delivery Time")]
        public DateTime? DeliveryTime
        {
            get { return EntityTenderBidder.DeliveryTime; }
            set { EntityTenderBidder.DeliveryTime = value; }
        }


        [Display(Name = "Warranty")]
        public string Warrant
        {
            get { return EntityTenderBidder.Warranty; }
            set { EntityTenderBidder.Warranty = value; }
        }


        [Required]
        public Guid? TermzOfPaymentID
        {
            get { return EntityTenderBidder.PaymentTermId; }
            set { EntityTenderBidder.PaymentTermId = value; }
        }
        public SelectList TermzOfPayments { get; set; }


        [Required]
        [Display(Name = "Transport Cost")]
        public decimal? TransportCost
        {
            get { return EntityTenderBidder.TransportCost; }
            set { EntityTenderBidder.TransportCost = value; }
        }


        [Required]
        [Display(Name = "Total cost")]
        public decimal? TotalCost
        {
            get { return EntityTenderBidder.TotalCost; }
            set { EntityTenderBidder.TotalCost = value; }
        }


        [Required]
        [Display(Name = "Others")]
        public string others
        {
            get { return EntityTenderBidder.Comments; }
            set { EntityTenderBidder.Comments = value; }
        }
    }

    public class RequestItems
    {
        public SCMS.Model.OrderRequestItem EntityRequestedItem { get; set; }

        public Guid ORItemId
        {
            get { return EntityRequestedItem.Id; }
            set { EntityRequestedItem.Id = value; }
        }

        public string ItemDescription { get { return EntityRequestedItem.ItemDescription; } set { EntityRequestedItem.ItemDescription = value; } }

        public Guid ORID { get { return EntityRequestedItem.OrderRequestId; } set { EntityRequestedItem.OrderRequestId = value; } }

        [Required]
        public decimal UnitCost { get; set; }
    }

    public class ViewTenders
    {
        public SCMS.Model.TenderAnalysi EntityTenderAnalysis { get; set; }

    }

    public class SelectItemsToConsider
    {
        public List<ORItemList> EntityORItems { get; set; }

        public List<OtherItemList> OtherItems { get; set; }

        public Guid TAID { get; set; }
    }

    public class GetSelectedItems
    {
        public SCMS.Model.TenderBidder EntityTenderBidder { get; set; }

        [Required]
        public Guid SupplierID
        {
            get { return EntityTenderBidder.SupplierId; }
            set { EntityTenderBidder.SupplierId = value; }
        }
        public SelectList Suppliers { get; set; }

        public List<DisplaySelectedItems> Itemselected { get; set; }

        public List<DisplayOtherSelectedItems> OtherNoneListItems { get; set; }

        public List<OtherSelectedListItems> OtherListItemsz { get; set; }

        public bool IsSupplierSaved { get; set; }

        public string UserMessage { get; set; }

        public TenderAnalysis ModelTA { get; set; }

        public Guid TAid { get; set; }
    }

    public class DisplaySelectedItems
    {
        public Model.TenderBidderQuote EntityTBQuote { get; set; }

        public Guid ORItemID { get { return EntityTBQuote.OrderRequestItemId; } set { EntityTBQuote.OrderRequestItemId = value; } }

        public string ItemName { get; set; }

        [Required]
        public decimal UnitCost { get { return EntityTBQuote.UnitPrice; } set { EntityTBQuote.UnitPrice = value; } }
    }

    public class DisplayOtherSelectedItems
    {
        public string ItemName { get; set; }

        [Required]
        public decimal UnitCost { get; set; }

        public string ItemValue { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime? DeliveryDate { get; set; }
    }

    public class OtherSelectedListItems
    {

        public string ItemName { get; set; }

        [Required]
        public Guid ItemID { get; set; }
        public SelectList ItemsList { get; set; }

    }

    public class ORItemList
    {
        public Model.OrderRequestItem EntityORItem { get; set; }

        public Guid ORItemID { get { return EntityORItem.Id; } set { EntityORItem.Id = value; } }

        public string ItemName { get { return EntityORItem.Item.Name; } set { EntityORItem.Item.Name = value; } }

        public bool AddToList { get; set; }
    }

    public class OtherItemList
    {
        public string Name { get; set; }
        public bool AddTOList { get; set; }

    }

    public class SelectEvaluators
    {
        public SCMS.Model.TenderEvaluator EntityTenderEvaluator { get; set; }

        [Required]
        public int? Priority
        {
            get { return EntityTenderEvaluator.Priority; }
            set { EntityTenderEvaluator.Priority = value; }
        }

        [Required]
        public Guid? StaffID
        {
            get { return EntityTenderEvaluator.StaffId; }
            set { EntityTenderEvaluator.StaffId = value; }
        }

        public SelectList Staffs { get; set; }

        public SelectList ApprovalLevels { get; set; }

        public string UserMessage { get; set; }

        public int status { get; set; }

        public List<TenderEvaluator> Approvers { get; set; }

        public TenderEvaluator Authorizer { get; set; }
    }

    public class SelectAuthorizer
    {
        public SCMS.Model.TenderEvaluator EntityTenderEvaluator { get; set; }

        public SelectList Authorizers { get; set; }
    }

    public class ApprovalLevel
    {
        public int Level { get; set; }
    }

    public class SearchTA
    {
        public Model.TenderAnalysi EntityTA { get; set; }

        public string TANumber { get { return EntityTA.RefNumber; } set { EntityTA.RefNumber = value; } }
    }

    #region Frank's Version

    public class TenderAnalysis
    {
        public SCMS.Model.TenderAnalysi EntityTenderAnalysis { get; set; }

        public Guid TenderAnalysisId { get; set; }

        public bool CanEdit { get; set; }

        public bool HasSuppliers { get; set; }

        public List<string> SupplierName
        {
            get;
            set;
        }

        public List<Guid> SupplierID
        {
            get;
            set;
        }

        public List<TenderItem> TBiderQuotes { get; set; }

        public List<string> ShippingTerm
        {
            get;
            set;
        }

        public List<DateTime> DeliveryTime
        {
            get;
            set;
        }

        public List<string> Warrant
        {
            get;
            set;
        }

        public List<string> TermzOfPayment
        {
            get;
            set;

        }

        public List<decimal> TransportCost
        {
            get;
            set;
        }

        public List<decimal> TotalCost
        {
            get;
            set;
        }

        public List<Guid> OtherID
        {
            get;
            set;
        }

        public List<string> others
        {

            get;
            set;
        }

        public List<Guid> TenderBidderId { get; set; }

        //Below are fields for evaluation and authorization
        [Required(ErrorMessage = "Supplier is required.")]
        public Guid RecommendedSupplierId { get; set; }
        public SelectList Suppliers { get; set; }

        public string Remarks { get; set; }

        public List<TenderEvaluator> TenderEvaluators { get; set; }
    }

    public class TenderItem
    {
        public SCMS.Model.OrderRequestItem EntityRequestedItem { get; set; }

        public Guid ORItemId
        {
            get { return EntityRequestedItem.Id; }
            set { EntityRequestedItem.Id = value; }
        }

        public string ItemDescription { get { return EntityRequestedItem.Item.Name; } set { EntityRequestedItem.Item.Name = value; } }

        public Guid ORID { get { return EntityRequestedItem.OrderRequestId; } set { EntityRequestedItem.OrderRequestId = value; } }

        public List<decimal> UnitCost { get; set; }
    }


    public class RejectTA
    {
        [Required]
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedAuthRemarks { get; set; }
    }

    #endregion

    public static class TAExtension
    {
        public static TenderAnalysis PrepareTAModel(Guid TAId, ITAnalysisService taService)
        {
            Models.TenderAnalysis modelTA = new Models.TenderAnalysis();
            modelTA.TenderAnalysisId = TAId;
            modelTA.HasSuppliers = false;
            List<TenderBidder> TBList = taService.GetTenderBidderColl(TAId);
            List<Models.TenderItem> tenderItems = new List<TenderItem>();
            modelTA.EntityTenderAnalysis = taService.GetTenderAnalysisEntity(TAId);
            modelTA.TenderEvaluators = taService.GetTenderEvaluators(TAId);
            Models.TenderItem tenderItem;
            List<TenderBidderQuote> tbQuotes;
            PopulateTALists(modelTA, TBList);
            if (TBList.Count > 0)
            {
                modelTA.HasSuppliers = true;
                tbQuotes = taService.GetTenderBidderQuoteColl(TBList[0].Id);
                foreach (TenderBidderQuote tbQuote in tbQuotes)
                {
                    tenderItem = new TenderItem();
                    tenderItem.UnitCost = new List<decimal>();
                    tenderItem.EntityRequestedItem = tbQuote.OrderRequestItem;
                    tenderItem.UnitCost.Add(tbQuote.UnitPrice * tbQuote.OrderRequestItem.Quantity);
                    tenderItems.Add(tenderItem);
                }
            }

            for (int i = 1; i < TBList.Count; i++)
            {
                tbQuotes = taService.GetTenderBidderQuoteColl(TBList[i].Id);
                for (int j = 0; j < tbQuotes.Count; j++)
                {
                    tenderItems[j].UnitCost.Add(tbQuotes[j].UnitPrice * tbQuotes[j].OrderRequestItem.Quantity);
                }
            }
            modelTA.TBiderQuotes = tenderItems;
            return modelTA;
        }

        private static void PopulateTALists(SCMS.UI.Models.TenderAnalysis ta, List<TenderBidder> tbList)
        {
            bool shipT, payT, warranty, delivTime, comments, transCost;
            shipT = payT = warranty = delivTime = comments = transCost = false;
            ta.SupplierName = new List<string>();
            ta.SupplierID = new List<Guid>();
            ta.ShippingTerm = new List<string>();
            ta.TermzOfPayment = new List<string>();
            ta.Warrant = new List<string>();
            ta.DeliveryTime = new List<DateTime>();
            ta.others = new List<string>();
            ta.TransportCost = new List<decimal>();
            ta.OtherID = new List<Guid>();
            ta.TenderBidderId = new List<Guid>();
            ta.TotalCost = new List<decimal>();

            foreach (TenderBidder tb in tbList)
            {
                ta.TenderBidderId.Add(tb.Id);
                ta.SupplierName.Add(tb.Supplier.Name);
                ta.SupplierID.Add(tb.SupplierId);
                if (tb.ShippingTermId != null)
                {
                    shipT = true;
                    ta.ShippingTerm.Add(tb.ShippingTerm.Name);
                }
                else
                    ta.ShippingTerm.Add(string.Empty);

                if (tb.PaymentTermId != null)
                {
                    payT = true;
                    ta.TermzOfPayment.Add(tb.PaymentTerm.Code);
                }
                else
                    ta.TermzOfPayment.Add(string.Empty);

                if (tb.Warranty != null)
                {
                    warranty = true;
                    ta.Warrant.Add(tb.Warranty);
                }
                else
                    ta.Warrant.Add(string.Empty);

                if (tb.DeliveryTime != null)
                {
                    delivTime = true;
                    ta.DeliveryTime.Add((DateTime)tb.DeliveryTime);
                }
                else
                    ta.DeliveryTime.Add(DateTime.MinValue);

                if (tb.Comments != null)
                {
                    comments = true;
                    ta.others.Add(tb.Comments);
                }
                else
                    ta.others.Add(string.Empty);

                if (tb.TransportCost != null)
                {
                    transCost = true;
                    ta.TransportCost.Add((decimal)tb.TransportCost);
                }
                else
                    ta.TransportCost.Add(0);
                ta.OtherID.Add(tb.Id);
                ta.TotalCost.Add((decimal)tb.TotalCost);
            }

            //Clear lists that have nothing completely
            if (!shipT) ta.ShippingTerm.Clear();
            if (!payT) ta.TermzOfPayment.Clear();
            if (!warranty) ta.Warrant.Clear();
            if (!delivTime) ta.DeliveryTime.Clear();
            if (!comments) ta.others.Clear();
            if (!transCost) ta.TransportCost.Clear();
        }
    }

}

